use bytemuck::{cast_slice, NoUninit};
use wgpu::util::DeviceExt;


#[derive(NoUninit, Clone, Copy)]
#[repr(C)]
pub struct Vertex {
    pub position: [f32; 3],
    pub normal: [f32; 3],
}

pub fn plane_vertices() -> Vec<Vertex> {
    vec![
        Vertex {
            position: [-0.5, 0.0, -0.5], normal: [0.0, 1.0, 0.0],
        },
        Vertex {
            position: [0.5, 0.0, -0.5], normal: [0.0, 1.0, 0.0],
        },
        Vertex {
            position: [-0.5, 0.0, 0.5], normal: [0.0, 1.0, 0.0],
        },
        Vertex {
            position: [0.5, 0.0, 0.5], normal: [0.0, 1.0, 0.0],
        },
        Vertex {
            position: [-0.5, 0.0, 0.5], normal: [0.0, 1.0, 0.0],
        },
        Vertex {
            position: [0.5, 0.0, -0.5], normal: [0.0, 1.0, 0.0],
        },
    ]
}

pub struct Mesh {
    pub vertices: Vec<Vertex>,
    pub(crate) buffer: wgpu::Buffer,
}
impl Mesh {
    pub fn from_vertices(device: &wgpu::Device, vertices: Vec<Vertex>) -> Self {
        let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: None,
            contents: cast_slice(&vertices),
            usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
        });
        Self {
            vertices, buffer
        }
    }
    pub fn plane(device: &wgpu::Device) -> Self {
        Self::from_vertices(device, plane_vertices())
    }
}